﻿using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class SliderBinder : MonoBehaviour
{
    [SerializeField]
    private Slider slider;
    [SerializeField]
    private TextMeshProUGUI text;

    private float _curValue;

    public float Value 
    { 
        get => _curValue; 
        set 
        {
            if (slider == null) return;
            _curValue = Mathf.Clamp(value, slider.minValue, slider.maxValue);
            slider.value = _curValue;
            text.text = _curValue.ToString("f0");
        }
    }

    public event Action<float> OnValueChange;

    private void Awake()
    {
        slider.onValueChanged.AddListener(OnChange);
        _curValue = slider.value;
    }

    private void OnChange(float value) 
    {
        _curValue = slider.value;
        text.text = _curValue.ToString("f0");
        OnValueChange?.Invoke(value);
    }

}
